﻿using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour 
{
	public int speed;
	public float damping  = 5.0f;
	protected Animator animator;

	public bool facingLeft = true;

	public bool blackActive = false;

	public AudioSource walkSound;
	public AudioSource changeSound;

	public Transform whiteGuy, blackGuy;
	public float maxDistance = 2.0f;
	protected bool jogadorPerto = false;

	protected bool muda = false;

	/// <summary>
	/// The last position.
	/// 0 = down; 1 = right; 2 = left; 3 = up;
	/// </summary>
	protected int lastPosition;

	void Awake()
	{
		animator = GetComponent<Animator>();
	}
	// Use this for initialization
	void Start () 
	{

	}
	
	// Update is called once per frame
	protected void Update () 
	{

		if (Input.GetButtonDown("Jump"))
		{
			ChangeActive ();

			if (!changeSound.isPlaying)
				changeSound.Play();
		}

		float distance = Vector3.Distance(blackGuy.transform.position,whiteGuy.position);
		if (distance < maxDistance)
		{
			jogadorPerto = true;
		}
		else
		{
			jogadorPerto = false;
		}	
		
		if (!jogadorPerto)
		{
			GameObject.Find("Ampulheta").GetComponent<Animator>().speed += 0.001f;
			GameObject.Find("Ampulheta").GetComponent<SpriteRenderer>().renderer.enabled = true;
			if (GameObject.Find("Ampulheta").GetComponent<Animator>().speed >= 5.0f)
			{
				//game over;
				Application.LoadLevel(1);
			}
		}
		else
		{
			GameObject.Find("Ampulheta").GetComponent<SpriteRenderer>().renderer.enabled = false;
			GameObject.Find("Ampulheta").GetComponent<Animator>().speed = 1.0f;
		}
	}

	protected void Movement(string horizontalAxis, string verticalAxis)
	{
		float moveVertical = 0;
		float moveHorizontal = 0;
		GameObject cam = GameObject.Find("Main Camera");

		moveVertical = Input.GetAxis (verticalAxis)*Time.deltaTime*speed;
		moveHorizontal = Input.GetAxis (horizontalAxis)*Time.deltaTime*speed;

		Vector3 cameraPosition = new Vector3(transform.position.x, transform.position.y, -10.0f);
		cam.transform.position  = Vector3.Lerp(cam.transform.position , cameraPosition, Time .deltaTime  * damping );


		if(moveVertical < 0)
		{
			this.transform.position += new Vector3(0,moveVertical,0f);
			animator.SetInteger("Vertical", 3);
			animator.SetInteger("Horizontal", 0);
			lastPosition = 0;
			if (!walkSound.isPlaying)
			{
				walkSound.Play();
			}
		}
		else if(moveVertical > 0)
		{
			this.transform.position += new Vector3(0,moveVertical,0f);
			animator.SetInteger("Vertical", 2);
			animator.SetInteger("Horizontal", 0);
			lastPosition = 3;
			if (!walkSound.isPlaying)
			{
				walkSound.Play();
			}
		}
		else if(moveHorizontal > 0)
		{
			this.transform.position += new Vector3(moveHorizontal,0,0f);
			animator.SetInteger("Horizontal", 1);
			animator.SetInteger("Vertical", 0);
			lastPosition = 2;
			if (!walkSound.isPlaying)
			{
				walkSound.Play();
			}
		}
		else if(moveHorizontal < 0)
		{
			this.transform.position += new Vector3(moveHorizontal,0,0f);
			animator.SetInteger("Horizontal", 1);
			animator.SetInteger("Vertical", 0);
			lastPosition = 1;
			if (!walkSound.isPlaying)
			{
				walkSound.Play();
			}
		}
		else
		{
			animator.SetInteger("Vertical", 0);
			animator.SetInteger("Horizontal", 0);
		}

		if (moveHorizontal > 0 && facingLeft) 
		{
			Flip ();
		}
		else if(moveHorizontal < 0 && !facingLeft)
		{
			Flip ();
		}
	}

	void Flip ()
	{
		facingLeft = !facingLeft;

		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
	}

	protected void ChangeActive()
	{

		blackActive = !blackActive;
		GameObject.Find("Main Camera").GetComponent<TileManager>().ChangeActive();

	}

}
